package com.hlxj.license.client.util;

import java.io.*;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @功能:【FileUtil 】
 * @作者:代守诚
 * @日期:2019/5/7
 * @时间:14:08
 */
public class FileUtil {
    /**
     * 获得类的基路径，打成jar包也可以正确获得路径
     * @return
     */
    public static String getBasePath(){
        /*
        /D:/zhao/Documents/NetBeansProjects/docCompare/build/classes/
        /D:/zhao/Documents/NetBeansProjects/docCompare/dist/bundles/docCompare/app/docCompare.jar
        */
        String filePath = FileUtil.class.getProtectionDomain().getCodeSource().getLocation().getFile();


        if (filePath.endsWith(".jar")){
            filePath = filePath.substring(0, filePath.lastIndexOf("/"));
            try {
                filePath = URLDecoder.decode(filePath, "UTF-8"); //解决路径中有空格%20的问题
            } catch (UnsupportedEncodingException ex) {

            }

        }
        File file = new File(filePath);
        filePath = file.getAbsolutePath();
        return filePath;
    }

    /**
     * 创建文件并写入
     * @param fileName 文件名
     * @param fileContent 文件内容
     * @return
     */
    public static boolean createFile(String fileName, String fileContent) {
        Boolean flag = false;
        String fileNameTemp = getBasePath() + "\\" + fileName + ".txt";//文件路径 + 名称 + 文件类型
        System.out.println(fileNameTemp);
        File file = new File(fileNameTemp);

        FileOutputStream fos = null;
        PrintWriter pw = null;

        try {

            if(!file.exists()) {
                file.createNewFile();
            }

            fos = new FileOutputStream(file);
            pw = new PrintWriter(fos);
            pw.write(fileContent);
            pw.flush();
            flag = true;

        }catch (IOException io) {
            io.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(pw != null) {
                    pw.close();
                }

                if(fos != null) {
                    fos.close();
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }

        return flag;
    }

    /**
     * @param fileName 文件名
     * @return
     */
    public static String readFileContent(String fileName) {
        StringBuilder result = new StringBuilder();
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;

        try {
            File file = new File(getBasePath() +  "\\" + fileName + ".txt");
            System.out.println(getBasePath() +  "\\" + fileName + ".txt");
            fis = new FileInputStream(file);
            isr = new InputStreamReader(fis);
            br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                result.append(line);
            }

        }catch (IOException io) {
            io.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(br != null) {
                    br.close();
                }

                if(isr != null) {
                    isr.close();
                }

                if(fis != null) {
                    fis.close();
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }

        return result.toString();
    }

    /**
     * map数据写入
     * @param fileName
     * @param contentMap
     * @return
     */
    public static boolean writeFile(String fileName, Map<String, String> contentMap) {
        Boolean flag = false;
        String fileNameTemp = getBasePath() + "\\" + fileName + ".txt";//文件路径 + 名称 + 文件类型
        System.out.println(fileNameTemp);
        File file = new File(fileNameTemp);

        FileOutputStream fos = null;
        PrintWriter pw = null;

        try {

            if(!file.exists()) {
                file.createNewFile();
            }

            //换行符
            String line = System.getProperty("line.separator");
            StringBuilder strBuilder = new StringBuilder();

            Iterator iterator = contentMap.entrySet().iterator();
            while(iterator.hasNext()) {
                Map.Entry entry = (Map.Entry) iterator.next();
                strBuilder.append(entry.getKey()).append("=").append(entry.getValue()).append(line);
            }

            fos = new FileOutputStream(file);
            pw = new PrintWriter(fos);
            pw.write(strBuilder.toString());
            pw.flush();
            flag = true;

        }catch (IOException io) {
            io.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(pw != null) {
                    pw.close();
                }

                if(fos != null) {
                    fos.close();
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }

        return flag;
    }

    /**
     * map数据读取
     * @param fileName
     * @return
     */
    public static Map<String, String> readMapContent(String fileName) {
        Map<String, String> resultMap = new HashMap<>();
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;

        try {
            File file = new File(getBasePath() +  "\\" + fileName + ".txt");
            System.out.println(getBasePath() +  "\\" + fileName + ".txt");
            fis = new FileInputStream(file);
            isr = new InputStreamReader(fis);
            br = new BufferedReader(isr);
            String line = null;
            int rowLine = 1;
            while ((line = br.readLine()) != null) {
                System.out.println("rowLine = " + rowLine + ";line" + line);
                String strArray[] = line.split("=");
                resultMap.put(strArray[0], strArray[1]);
                rowLine++;
            }

        }catch (IOException io) {
            io.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(br != null) {
                    br.close();
                }

                if(isr != null) {
                    isr.close();
                }

                if(fis != null) {
                    fis.close();
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }

        return resultMap;
    }


    public static void main(String[] args) throws Exception {
//        System.out.println(createFile("license", "22测试代码3333"));
//        System.out.println(readFileContent("license"));
        Map<String, String> map = new HashMap<>();
        map.put("cpu", "sw2srdx");
        map.put("内存", "sssss");
        map.put("endTime", "2017-08-08 12:21:56");

        System.out.println(writeFile("testaa", map));
        Map<String, String> infoMap = readMapContent("testaa");
        System.out.println(infoMap);
        String endTime = infoMap.get("endTime");
        System.out.println(endTime);

        System.out.println(map);
    }


}
